#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os, os.path, sys
import md5

import tempfile
import unittest

from potherb import log
from potherb.cell import CellCache
from potherb.key import KeySmith
from potherb.const import COLUMN_NOT_FOUND,COLUMN_FOUND,COLUMN_DELETED,ROW_DELETED

DASH = '-'

def makeData(key) :
    return DASH.join([key] * 4)
    ## makeData()


class   TestCellCache(unittest.TestCase) :
    def setUp(self) :
        self.store_dir = tempfile.mkdtemp(suffix='.dir', prefix='cache_')
        log.debug('store_dir=%s', self.store_dir)
        ## setUp()

    def removeDirs(self, dir_name) :
        items = os.listdir(dir_name)
        for item in items :
            dir_item = '%s/%s' % (dir_name, item)
            if os.path.isdir(dir_item) :
                self.removeDirs(dir_item)
            else :
                #log.debug('CONTENT of file %s', dir_item)
                #for line in file(dir_item) :
                #    log.debug('%s', line.strip())
                log.debug('remove file %s', dir_item)
                os.remove(dir_item)
        if os.path.exists(dir_name) :
            log.debug('remove directory %s', dir_name)
            os.removedirs(dir_name)
        ## removedirs()

    def tearDown(self) :
        if os.path.exists(self.store_dir) :
            self.removeDirs(self.store_dir)
        ## tearDown()

    def test_001(self) :
        cache = CellCache()
        
        # test INSERT operation
        timestamp = 1000
        for row_key in ('foo', 'baz', 'bar') :
            data = '%s--DATA--%d' % (row_key, timestamp)
            for flag in (KeySmith.FLAG_INSERT, ) :
                smith = KeySmith(row_key=row_key, flag=flag, timestamp=timestamp)
                cache.insert(row_key, smith.key, data)

            timestamp += 1

        # test findColumn operation
        timestamp = 1000
        for row_key in ('foo', 'baz', 'bar') :
            data = '%s--DATA--%d' % (row_key, timestamp)
            smith = KeySmith(row_key=row_key)
            column_key = smith.getColumnKey()
            _status,_data,_timestamp = cache.findColumn(row_key, column_key)
            self.assertEqual(COLUMN_FOUND, _status)
            self.assertEqual(data, _data)
            self.assertEqual(timestamp, _timestamp)

            timestamp += 1

        for row_key in ('fooN', 'bazN', 'barN') :
            smith = KeySmith(row_key=row_key)
            column_key = smith.getColumnKey()
            _status,_data,_timestamp = cache.findColumn(row_key, column_key)
            self.assertEqual(COLUMN_NOT_FOUND, _status)

        # test iterator operation
        count = 0
        cache_iterator = iter(cache)
        for key,data in cache_iterator :
            smith = KeySmith()
            smith.desemble(key)
            if count == 0 :
                self.assertEqual('foo', smith.row_key)
            elif count == 1 :
                self.assertEqual('baz', smith.row_key)
            elif count == 2 :
                self.assertEqual('bar', smith.row_key)
            else :
                self.fail('Too many records')
            count += 1

        self.assertEqual(count, 3)

        del cache
        ## test_001()

    ## class TestCellCache

#######################################################################

def test_suite():
    suite = unittest.TestSuite()

    suite.addTest(unittest.makeSuite(TestCellCache))

    return suite

if  __name__ == '__main__' :
    #log.setLogPath('/dev/null')
    log.enableDebug()

    unittest.main()

